package manager;

import java.util.LinkedList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sun.xml.wss.saml.internal.saml11.jaxb10.Object;

import dto.ContattoDTO;
import entities.Contatto;

/**
 * Session Bean implementation class GestoreContatti
 */
@Stateless
@LocalBean
public class GestoreContatti implements GestoreContattiLocal {

    /**
     * Default constructor. 
     */
	@PersistenceContext
	private EntityManager em;
	
    public GestoreContatti() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public List<ContattoDTO> getAll(String utenteid) {
		Query q = em.createNamedQuery("getall");
		q.setParameter("nome", utenteid);
		//Object[] ol =  (Object[]) q.getResultList().toArray();
		List<ContattoDTO> cdto = new LinkedList<ContattoDTO>() ;
		for(int i=0;i<q.getResultList().toArray().length;i++){
			cdto.add(((Contatto)q.getResultList().toArray()[i]).getDTO());
		}
		return cdto;
	}

	@Override
	public void removeContatto(String utenteid, String contattoid) {
		Query q = em.createNamedQuery("remove");
		q.setParameter("utenteid", utenteid);
		q.setParameter("contattoid", contattoid);
		q.executeUpdate();
	}

}
